﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace ExcelExportLib
{
    /// <summary>Worksheet display options</summary>
    [XmlRoot("WorksheetOptions")]
    public class WorksheetOptions : IXmlSerializable
    {
        #region horisontal splitter parameters
        
        int? _SplitHorizontal = null;

        /// <summary>Bottom row number of top pane</summary>
        public int? SplitHorizontal
        {
            get { return _SplitHorizontal; }
            set 
            {
                _SplitHorizontal = value;
                if (value == null)
                    _TopRowBottomPane = null;
                else
                    if ((_TopRowBottomPane == null) || (_TopRowBottomPane < _SplitHorizontal))
                        _TopRowBottomPane = _SplitHorizontal;
            }
        }

        int? _TopRowBottomPane = null;

        /// <summary>Offset row of bottom frame </summary>
        /// <remarks>This is not the actual row number of the overall Excel document, but rather the index of the available rows for this pane. This number cannot exclude rows, it can merely set the offset scroll of this pane. (1) scrolls to the top of the frame (first row)</remarks>
        public int? TopRowBottomPane
        {
            get { return _TopRowBottomPane; }
            set 
            {
                if (value == null)
                    throw new ArgumentNullException("TopRowBottomPane", "Set SplitHorizontal = null if you need TopRowBottomPane == null ");
                _TopRowBottomPane = value; 
            }
        }
        
        #endregion

        #region vertical splitter parameters
        
        /// <summary>Right-most column number of left pane</summary>
        int? _SplitVertical = null;

        public int? SplitVertical
        {
            get { return _SplitVertical; }
            set 
            {
                _SplitVertical = value;
                if (value == null)
                    _LeftColumnRightPane = null;
                else
                    if ((_LeftColumnRightPane == null) || (_LeftColumnRightPane < _SplitVertical))
                        _LeftColumnRightPane = _SplitVertical;
            }
        }

        /// <summary>Offset column of right frame </summary>
        /// <remarks>This is not the actual column number of the overall Excel document, but rather the index of the available column for this pane. This number cannot exclude columns, it can merely set the offset scroll of this pane. (1) scrolls to the left of the frame (first column)</remarks>
        int? _LeftColumnRightPane = null;

        public int? LeftColumnRightPane
        {
            get { return _LeftColumnRightPane; }
            set 
            {
                if (value == null)
                    throw new ArgumentNullException("LeftColumnRightPane", "Set SplitVertical = null if you need LeftColumnRightPane == null ");

                _LeftColumnRightPane = value; 
            }
        }
        
        #endregion

        public int ActivePane = 0;
        
        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            throw new NotImplementedException();
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            throw new NotImplementedException();
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            //checking if options exists
            if (((SplitVertical == null) && (LeftColumnRightPane == null)) &&
                ((SplitHorizontal == null) && (TopRowBottomPane == null))) return; //nothing to save

            writer.WriteStartElement("WorksheetOptions");
            writer.WriteAttributeString("xmlns", null, null, "urn:schemas-microsoft-com:office:excel");
            writer.WriteElementString("Selected", null);
            writer.WriteElementString("FreezePanes", null);
            writer.WriteElementString("FrozenNoSplit", null);
            writer.WriteElementString("ActivePane", ActivePane.ToString());

            if ((SplitVertical != null) && (LeftColumnRightPane != null))
            {
                writer.WriteElementString("SplitVertical", SplitVertical.ToString());
                writer.WriteElementString("LeftColumnRightPane", LeftColumnRightPane.ToString());
            }
            if ((SplitHorizontal != null) && (TopRowBottomPane != null))
            {
                writer.WriteElementString("SplitHorizontal", SplitHorizontal.ToString());
                writer.WriteElementString("TopRowBottomPane", TopRowBottomPane.ToString());
            }
            writer.WriteEndElement();
            
        }

        #endregion
    }
}
